"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const mongodb = require("mongodb");
const Db_Name = 'metronic';
const Record_Sync_Table = "InspectSyncRecord";
const Mongo_Url = 'mongodb://localhost:27017';
const Line_Sync_Table = 'InspectLine';
/** 这个记录是否已经同步过 */
function isRecordSyncAlready(db, record) {
    return __awaiter(this, void 0, void 0, function* () {
        let collection = db.collection(Record_Sync_Table);
        let res = yield collection.findOne({ name: record.name, date: record.date, sectionName: record.section });
        return !!res;
    });
}
exports.isRecordSyncAlready = isRecordSyncAlready;
/** 将存的结果同步到mongo里去 */
function saveSyncRecord(db, record) {
    return __awaiter(this, void 0, void 0, function* () {
        let collection = db.collection(Record_Sync_Table);
        let res = yield collection.insertOne(record);
    });
}
exports.saveSyncRecord = saveSyncRecord;
/** 打开一个对mongodb的访问 */
function openMongoAsync() {
    return __awaiter(this, void 0, void 0, function* () {
        let client = yield mongodb.connect(Mongo_Url);
        let db = client.db(Db_Name);
        return { client, db };
    });
}
exports.openMongoAsync = openMongoAsync;
/** 关闭数据访问 */
function closeMongoAsync(client) {
    return __awaiter(this, void 0, void 0, function* () {
        yield client.close();
    });
}
exports.closeMongoAsync = closeMongoAsync;
/** 根据线路的名称获取线路数据 */
function getLineByNameAsync(db, lineName) {
    return __awaiter(this, void 0, void 0, function* () {
        let collection = db.collection(Line_Sync_Table);
        let line = yield collection.findOne({ name: lineName });
        return line;
    });
}
exports.getLineByNameAsync = getLineByNameAsync;
/** 直接更新数据库中的line
 * 颗粒度比较粗糙！！！ 呵呵
 */
function updateLineAysnc(db, lineName, newLine) {
    return __awaiter(this, void 0, void 0, function* () {
        let collection = db.collection(Line_Sync_Table);
        let line = yield collection.findOne({ name: lineName });
        if (!line) {
            yield collection.insertOne(newLine);
        }
        else {
            yield collection.findOneAndUpdate({ name: lineName }, newLine);
        }
    });
}
exports.updateLineAysnc = updateLineAysnc;
